package com.zapi.lottery.service;

import com.zapi.lottery.entity.LotteryPrize;
import com.zapi.lottery.entity.LotterySetting;
import com.zapi.lottery.entity.LotteryWinningNumber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 抽奖系统初始化服务
 * 在应用启动时自动初始化基础数据
 * @author: zhonglz
 * @DATE: 2024-06-09
 */
@Service
public class LotteryInitService implements CommandLineRunner {

    @Autowired
    private LotteryPrizeService lotteryPrizeService;

    @Autowired
    private LotterySettingService lotterySettingService;

    @Autowired
    private LotteryWinningNumberService lotteryWinningNumberService;

    @Override
    @Transactional
    public void run(String... args) throws Exception {
        initLotteryData();
    }

    /**
     * 初始化抽奖系统数据
     */
    private void initLotteryData() {
        try {
            // 1. 初始化抽奖范围设置
            initLotteryPrize();
            
            // 2. 初始化奖项设置
            initLotterySettings();
            
            // 3. 初始化中奖号码配置
            initWinningNumbers();
            
            System.out.println("✅ 抽奖系统数据初始化完成");
        } catch (Exception e) {
            System.err.println("❌ 抽奖系统数据初始化失败: " + e.getMessage());
        }
    }

    /**
     * 初始化抽奖范围设置
     */
    private void initLotteryPrize() {
        // 检查是否已有数据
        List<LotteryPrize> existingPrizes = lotteryPrizeService.list();
        if (!existingPrizes.isEmpty()) {
            System.out.println("📊 抽奖范围设置已存在，跳过初始化");
            return;
        }

        // 创建默认抽奖范围
        LotteryPrize prize = new LotteryPrize();
        prize.setMinNumber("000");
        prize.setMaxNumber("999");
        
        lotteryPrizeService.save(prize);
        System.out.println("✅ 抽奖范围设置初始化完成");
    }

    /**
     * 初始化奖项设置
     */
    private void initLotterySettings() {
        // 检查是否已有数据
        List<LotterySetting> existingSettings = lotterySettingService.list();
        if (!existingSettings.isEmpty()) {
            System.out.println("📊 奖项设置已存在，跳过初始化");
            return;
        }

        // 创建默认奖项设置
        List<LotterySetting> settings = Arrays.asList(
            createSetting("特等奖", "special", 1),
            createSetting("一等奖", "first", 2),
            createSetting("二等奖", "second", 3),
            createSetting("三等奖", "third", 5),
            createSetting("幸运奖", "lucky", 10)
        );

        lotterySettingService.saveBatch(settings);
        System.out.println("✅ 奖项设置初始化完成");
    }

    /**
     * 创建奖项设置
     */
    private LotterySetting createSetting(String prizeName, String awardCode, Integer totalWinners) {
        LotterySetting setting = new LotterySetting();
        setting.setPrizeName(prizeName);
        setting.setAwardCode(awardCode);
        setting.setTotalWinners(totalWinners);
        setting.setStatus(1); // 默认启用
        return setting;
    }

    /**
     * 初始化中奖号码配置
     */
    private void initWinningNumbers() {
        // 检查是否已有数据
        List<LotteryWinningNumber> existingNumbers = lotteryWinningNumberService.list();
        if (!existingNumbers.isEmpty()) {
            System.out.println("📊 中奖号码配置已存在，跳过初始化");
            return;
        }

        // 获取所有奖项设置
        List<LotterySetting> settings = lotterySettingService.list();
        
        for (LotterySetting setting : settings) {
            List<String> winningNumbers = getDefaultWinningNumbers(setting.getAwardCode());
            lotteryWinningNumberService.batchSaveWinningNumbers(setting.getId(), winningNumbers);
        }
        
        System.out.println("✅ 中奖号码配置初始化完成");
    }

    /**
     * 获取默认中奖号码
     */
    private List<String> getDefaultWinningNumbers(String awardCode) {
        List<String> numbers = new ArrayList<>();
        
        switch (awardCode.toLowerCase()) {
            case "special":
                numbers.addAll(Arrays.asList("088"));
                break;
            case "first":
                numbers.addAll(Arrays.asList("011"));
                break;
            case "second":
                numbers.addAll(Arrays.asList("066"));
                break;
            case "third":
                numbers.addAll(Arrays.asList("001"));
                break;
            case "lucky":
                numbers.addAll(Arrays.asList("100"));
                break;
            default:
                break;
        }
        
        return numbers;
    }
} 